package com.llk.p;

import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.*;

import javax.xml.bind.JAXBElement;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.util.List;

/**
 * 段落
 */
public class Test03 {


    public static void main(String[] args) throws Exception{
//        setRunPPR();
//        setRunFont();
        setCS();
    }

    //获取段落的文本内容
    public static void getPText() throws Exception {
        String filePath = "p/2.docx";
        String destPath = "p/dest/3.docx";
        WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(filePath));

        MainDocumentPart document = wPackage.getMainDocumentPart();
        //获取文档中的标签
        List<Object> content = document.getContent();
        P p = (P)content.get(0);
        //获取 p 标签的内容，即它的孩子标签
        List<Object> pContent = p.getContent();
        R r = (R)pContent.get(0);
        //获取 <w:r> 标签下的 <w:t> 标签
        List<Object> rContent = r.getContent();

        Text t = (Text)((JAXBElement)rContent.get(0)).getValue();
        System.out.println(t.getValue());
        t.setValue("新设置的文本内容");
        wPackage.save(new File(destPath));
    }

    //设置 <w:r> 的属性
    public static void setRunPPR () throws Exception{
        String filePath = "p/2.docx";
        String destPath = "p/dest/3.docx";
        WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(filePath));

        MainDocumentPart document = wPackage.getMainDocumentPart();
        //获取文档中的标签
        List<Object> content = document.getContent();
        P p = (P)content.get(0);
        //获取 p 标签的内容，即它的孩子标签
        List<Object> pContent = p.getContent();
        R r = (R)pContent.get(0);
        //获取 r 的属性
        RPr rPr = r.getRPr();
        //加粗
        rPr.setB(new BooleanDefaultTrue());
        //黄色高亮
        Highlight highlight = new Highlight();
        highlight.setVal("yellow");
        rPr.setHighlight(highlight);
        //斜体
        rPr.setI(new BooleanDefaultTrue());

        wPackage.save(new File(destPath));
    }

    public static void setRunFont () throws Exception{
        String filePath = "p/3.docx";
        String destPath = "p/dest/3.docx";
        WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(filePath));

        MainDocumentPart document = wPackage.getMainDocumentPart();
        //获取文档中的标签
        List<Object> content = document.getContent();
        P p = (P)content.get(0);
        //获取 p 标签的内容，即它的孩子标签
        List<Object> pContent = p.getContent();
        R r = (R)pContent.get(0);
        //获取 r 的属性
        RPr rPr = r.getRPr();
        //设置字体大小
//        HpsMeasure hpsMeasure = new HpsMeasure();
//        hpsMeasure.setVal( BigInteger.valueOf(144));
//        rPr.setSz(hpsMeasure);

        //设置字体
        RFonts rFonts = new RFonts();
        rFonts.setEastAsia("楷体");
        rFonts.setAscii("Times New Roman");
        rFonts.setHAnsi("新宋体");
        rFonts.setCs("Arial Unicode MS");
        rPr.setRFonts(rFonts);

        wPackage.save(new File(destPath));
    }

    public static void setCS () throws Exception{
        String filePath = "p/4.docx";
        String destPath = "p/dest/3.docx";
        WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(filePath));

        MainDocumentPart document = wPackage.getMainDocumentPart();
        //获取文档中的标签
        List<Object> content = document.getContent();
        P p = (P)content.get(0);
        //获取 p 标签的内容，即它的孩子标签
        List<Object> pContent = p.getContent();
        R r = (R)pContent.get(0);
        //获取 r 的属性
        RPr rPr = r.getRPr();
        rPr.setCs(new BooleanDefaultTrue());
        //加粗
        rPr.setBCs(new BooleanDefaultTrue());
        //斜体
        rPr.setICs(new BooleanDefaultTrue());

        HpsMeasure hpsMeasure = new HpsMeasure();
        hpsMeasure.setVal(BigInteger.valueOf(144));
        rPr.setSzCs(hpsMeasure);

        wPackage.save(new File(destPath));
    }

}
